package com.alpha.eceasy.mall.controller;

import com.alpha.eceasy.mall.api.entity.MallCommodityEvaluation;
import com.alpha.eceasy.core.R;
import com.alpha.eceasy.mall.service.MallCommodityEvaluationService;
import com.alpha.eceasy.mall.service.impl.MallCommodityEvaluationServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品评价
 *
 * @author huangpengfei
 * @since 2022-02-11 16:55:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/commodity-evaluation")
@Hidden
@Tag(description  = "commodity-evaluation", name = "商品评价")
public class MallCommodityEvaluationController {
	private final MallCommodityEvaluationServiceImpl mallCommodityEvaluationService;

	/**
	 * 分页查询
	 *
	 * @param page                    分页对象
	 * @param mallCommodityEvaluation 商品评价
	 * @return 分页结果
	 */
	@Operation(description = "分页查询", summary = "分页查询")
	@GetMapping("")
	public R<IPage<MallCommodityEvaluation>> getMallCommodityPage(Page<MallCommodityEvaluation> page, MallCommodityEvaluation mallCommodityEvaluation) {
		return R.ok(mallCommodityEvaluationService.page(page, Wrappers.query(mallCommodityEvaluation).eq("deleted", false)));
	}

	/**
	 * 通过id查询商品评价
	 *
	 * @param id id
	 * @return R 返回结果 商品评价
	 */
	@Operation(description = "通过id查询", summary = "通过id查询")
	@GetMapping("/{id}")
	public R<MallCommodityEvaluation> getById(@PathVariable("id") Long id) {
		return R.ok(mallCommodityEvaluationService.getById(id));
	}

	/**
	 * 通过商品id查询商品评价
	 *
	 * @param commodityId 商品id
	 * @return R 返回结果 商品评价
	 */
	@Operation(description = "通过id查询", summary = "通过id查询")
	@GetMapping("/commodityId/{commodityId}")
	public R<List<MallCommodityEvaluation>> getByCommodityId(@PathVariable("commodityId") Long commodityId) {
		return R.ok(mallCommodityEvaluationService.list(Wrappers.<MallCommodityEvaluation>lambdaQuery()
				.eq(MallCommodityEvaluation::getCommodityGuid, commodityId)
				.eq(MallCommodityEvaluation::getDeleted, false)
				.eq(MallCommodityEvaluation::getState, true)));
	}

	/**
	 * 新增商品评价
	 *
	 * @param mallCommodityEvaluation 商品评价
	 * @return R 返回结果 是否成功
	 */
	@Operation(description = "新增商品评价", summary = "新增商品评价")
	//@SysLog("新增商品评价")
	@PostMapping
	public R<Boolean> save(@RequestBody MallCommodityEvaluation mallCommodityEvaluation) {
		return R.ok(mallCommodityEvaluationService.save(mallCommodityEvaluation));
	}

	/**
	 * 修改商品评价
	 *
	 * @param mallCommodityEvaluation 商品评价
	 * @return R 返回结果 是否成功
	 */
	@Operation(description = "修改商品评价", summary = "修改商品评价")
	//@SysLog("修改商品评价")
	@PutMapping
	public R<Boolean> updateById(@RequestBody MallCommodityEvaluation mallCommodityEvaluation) {
		return R.ok(mallCommodityEvaluationService.updateById(mallCommodityEvaluation));
	}
}
